﻿using Digitalmes.ApiService.Hubs;
using Digitalmes.Scada.Events;
using MediatR;

namespace Digitalmes.ApiService.EventHandlers;

public class AfterScanBarcodeHandler : INotificationHandler<AfterScanBarcodeEvent>
{
    private readonly IHubContext<ChannelHub, IChannelClient> _hubContext;

    public AfterScanBarcodeHandler(IHubContext<ChannelHub, IChannelClient> hubContext)
    {
        _hubContext = hubContext;
    }

    public async Task Handle(AfterScanBarcodeEvent notification, CancellationToken cancellationToken)
    {
        ClentMessage message = new()
        {
            LineCode = notification.LineCode,
            OpCode = notification.OpCode,
            Barcode = notification.Barcode,
            State = notification.State,
            ErrorMessage = notification.ErrorMessage,
            InnerErrorCode = notification.InnerErrorCode,
            Target = notification.Target,
        };
        await _hubContext.Clients.All.ReceiveMessage(message);
    }
}
